//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES OR INDEMNITIES.
//
//
// (C) Copyright 2006 Marvell International Ltd.
// All Rights Reserved
//
//------------------------------------------------------------------------------
//
//  Header: pxa_usbohci.h
//
//  Defines the USB device controller CPU register layout and definitions.
//
#ifndef __MONAHANS_USBOHCI_H
#define __MONAHANS_USBOHCI_H
#include <xllp_usbohci.h>
//------------------------------------------------------------------------------
//
//  Type: PXA_USBDOHCI_REG    
//
//  Defines the USB OHCI host control register block.
//

typedef XLLP_USBOHCI_T  PXA_USBOHCI_REG;
typedef XLLP_USBOHCI_T *PPXA_USBOHCI_REG;


#include <Cmthread.h>
#include <CRegEdit.h>
#include "ipm_api.h"
//------------------------------------------------------------------------------

#define PXA_REG_DMA_BUFFER_PH_ADDR_VAL_NAME TEXT("DmaMemBase")
#define PXA_REG_DMA_BUFFER_PH_ADDR_VAL_LEN  sizeof(DWORD)
#define PXA_REG_DMA_BUFFER_LENGTH_VAL_NAME  TEXT("DmaMenLen")
#define PXA_REG_DMA_BUFFER_LENGTH_VAL_LEN  sizeof(DWORD)
#define REG_PHYSICAL_PAGE_SIZE TEXT("PhysicalPageSize")
#define REG_PHYSICAL_PAGE_SIZE_LEN sizeof(DWORD)

///USB HOST SOC PDD Driver class
class SOhcdPdd : public CRegistryEdit, public CMiniThread { 
public:

    ///Contruct function
    ///@param   lpActiveRegistry    [IN]Active Key for the interface driver
    SOhcdPdd (LPCTSTR lpActiveRegistry);

    ///Deconstruction Function
    ///Deregister IPM Client. Release all event handle and unmap all register map address. 
    virtual ~SOhcdPdd ();

    ///Driver initialize function
    ///Map All Register address and register as IPM Client
    virtual BOOL Init();

    ///Turn ON USB Host Clock in Clock Mamanage
    void TurnOnUSBHostClocks();
    ///Turn OFF USB Host Clock in Clock Mamanage
    void TurnOffUSBHostClocks();

    ///Config USB Host PWR PIN
    virtual void SetupUSBHostPWR(int port) = 0;
    ///Config USB Host PEN PIN
    virtual void SetupUSBHostPEN(int Port) =0;

    /// OHCI Configuration
    /// Install ISR, Get registry value, Allocate DAM Buffer. 
    /// @remarks  Set Capablity HCD_ROOT_HUB_INTERRUPT. When OTG enable, OHCI MDD always use polling to get root hub status. 
    virtual BOOL InitializeOHCI();
protected:

    /// Reset OHCI
    /// Call XllpUsbHPowerUp
    virtual BOOL OHCI_Reset();

    /// Enable Other USB Host PDD interrupt (Buffer-Access, Remote Wakeup, Port Resume, OHCI-Initialized, USB Port over current)
    virtual BOOL InitPddInterrupts();

    /// Disable Other USB Host PDD Interrupt (Buffer-Access, Remote Wakeup, Port Resume, OHCI-Initialized, USB Port over current)
    virtual void DisablePddInterrupts() { 
        m_pDCUSBOHCIReg->uhchie = 0 ; // Mask All PDD interrupt.
    };

private:
    /// PDD IST
    virtual DWORD ThreadRun() ;  
public:
    /// Call this function when system resume
    /// Call XllpUsbhPowerUp and HcdMdd_Powerup
    virtual void PowerUp();

    /// Cal this function when system resume
    /// Call XllpUsbhPowerDown only
    virtual void PowerDown();

    /// Power Up PDD
    virtual DWORD InitiatePowerUp();
    
protected:
    volatile P_XLLP_CLKMGR_T m_pDCCLKReg;               ///<! Clock unit base virtual address
    volatile PPXA_USBOHCI_REG m_pDCUSBOHCIReg;     ///<! USB OHCI base virtual address

    DMA_ADAPTER_OBJECT  m_AdapterObject;                ///<! DMA Adapter Object
    BOOL                m_bIsBuiltInDma;                ///<! USE special DMA buffer, such as ISRAM
    PHYSICAL_ADDRESS    m_DmaPhysicalAddr;              ///<! DMA Buffer phycial address
    PVOID               m_pvDmaVirtualAddress;          ///<! DMA buffers as seen by the CPU
    DWORD               m_dwDamBufferSize;              ///<! DAM buffer size
    LPVOID              m_pobMem;                       ///<! HCD Memory object
    
    DWORD   m_dwSysIntr;                                ///<! OHCI systerm interrupt number
    
    DWORD m_dwSysIntrNoOHCI;                            ///<! NO-OHCI System interrupt number 
    HANDLE  m_IsrHandle;                                ///<! Install isr handle

    LPVOID  m_pobOhcd ;                                 ///<! PDD object
    CLIENT_ID m_IPMID;                                  ///<! IPM Client ID

    // Registry Key.
    LPTSTR  m_lpDriverReg;                              ///<! Driver registry 
    HANDLE  m_hParentBusHandle;                         ///<! Parenet Bus Handle
    HANDLE  m_hISTEvent;                                ///<! No-OHCI interrupt event
    
    XLLP_USBH_HANDLE_T m_XllpHandle;                    ///<! XLLP Handle. All xllp function call need such data
    XLLP_PLATFORM_USBH_T m_Platform;                    ///<! Platform specific USB Host primitives
};


 /// Create OHCI Object
 /// Implement it at platform related code. 
 /// @param     lpActiveRegistry    [IN]Active Key for the interface driver
 /// @return  SOhcdPdd object
SOhcdPdd * CreatePXAOhci(LPCTSTR lpActiveRegistry) ;

#endif 
